//
//  AMPWDSettingsViewController.m
//  WeiDu_1_0
//
//  Created by foguo-mac-1 on 01/07/16.
//  Copyright (c) 2016 WeiDu Energy Tech. All rights reserved.
//

#import "HOPWDSettingsViewController.h"
#import "HOButton.h"
#import "HOConstants.h"
#import "Logger.h"
#import "UIDevice+IdentifierAddition.h"
#import "HOLoginViewController.h"
#import "HOUIMgr.h"
#import "HOUtils.h"
#import "HOFillInfoViewController.h"
#import "HOUserInfoDataModule.h"
#import "HOTabViewController.h"
#import "NSString+MD5Addition.h"

@interface HOPWDSettingsViewController ()<HOEditTableViewCellDelegate>
{
}
@property(nonatomic, retain)UITextField *oldPwdTextField;
@property(nonatomic, retain)UITextField *pwdTextField;
@property(nonatomic, retain)UITextField *pwdConfirmTextField;
@end

@implementation HOPWDSettingsViewController
@synthesize messageID;
@synthesize code;
@synthesize phoneNumber;
@synthesize replacedPassword;
@synthesize replacedPasswordCheck;
@synthesize settingsViewController;
@synthesize isResetPwd;
@synthesize loginViewController;
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self initTableView];
//    baseTableview.scrollEnabled = NO;
	[self registerKeyboard];
	[self registerTapGesture];
	
	baseTableview.tableFooterView = [self getFooterViewWithTitle:NSLocalizedString(@"确认修改", @"确认修改")];
}


- (void)didClickFooterButton:(UIButton *)btn
{
	NSString *oldPwd = [self getTrimingCharaFromTextField:self.oldPwdTextField];
	NSString *replacedPassword = [self getTrimingCharaFromTextField:self.pwdTextField];
	NSString *replacedPasswordAgain = [self getTrimingCharaFromTextField:self.pwdConfirmTextField];
	if(oldPwd.length < 6 || oldPwd.length > 20)
	{
		[super popupHintView:NSLocalizedString(@"请输入6-20位原密码！", @"请输入6-20位原密码！")];
		return;
	}
	
	if(replacedPassword.length < 6 || replacedPassword.length > 20)
	{
		[super popupHintView:NSLocalizedString(@"请输入6-20位新密码！", @"请输入6-20位新密码！")];
		return;
	}
	if(replacedPasswordAgain.length < 6 || replacedPasswordAgain.length > 20)
	{
		[super popupHintView:NSLocalizedString(@"请再输入6-20位新密码！", @"请再输入6-20位新密码！")];
		return;
	}
	if(![replacedPassword isEqualToString:replacedPasswordAgain])
	{
		[super popupHintView:NSLocalizedString(@"新密码输入不同，请重新输入！", @"新密码输入不同，请重新输入！")];
		return;
	}

	[self sendRequestWithParams:@{kHO_FIELD_Action    : kHO_ACTION_ResetPassword,
										kHO_FIELD_Mobile    : [HOUtils getValideString:self.phoneNumber],
										kHO_FIELD_OriginalPassword  : [oldPwd stringFromMD5],
										kHO_FIELD_NewPassword  : [replacedPassword stringFromMD5]}];
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
	[self hideKeyboard];
}

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden = NO;
    self.tabBarController.tabBar.hidden = YES;
	if(self.navTitle)
	{
		[self setNavigationItemTitle:self.navTitle];
	}
	else
	{
		[self setNavigationItemTitle:NSLocalizedString(@"修改密码", @"修改密码")];
	}
//	[self displayRightButtomItemWithTitle:NSLocalizedString(@"确认修改", @"确认修改")];
    [super viewWillAppear:animated];
    [self registerTapGesture];
	[self registerKeyboard];
	[baseTableview reloadData];
	[self.pwdTextField becomeFirstResponder];
	
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self unregisterTapGesture];
    
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

- (void)hideKeyboard
{
    [self.pwdTextField resignFirstResponder];
	[self.pwdConfirmTextField resignFirstResponder];
	[self.oldPwdTextField resignFirstResponder];
}


- (void)dealloc
{
	self.pwdConfirmTextField = nil;
	self.pwdTextField = nil;
	self.oldPwdTextField = nil;
    [super dealloc];
}

#pragma mark - -- UITableViewDelegate/UITableViewDataResource implement methods
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
	return IS_IPHONE_4_SCREEN ? 44 : 50;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return 3;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return SECTION_PADDING / 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	CGFloat cellHeight = [self tableView:tableView heightForRowAtIndexPath:indexPath];
	HOEditTableViewCell *cell = [super tableView:tableView cellForEditRowAtIndexPath:indexPath];
	cell.accessoryType = UITableViewCellAccessoryNone;
	[cell initViewsWithHeight:cellHeight withWidth:tableView.frame.size.width withDelegate:self];
	[cell setTextEditing:YES];
	[cell setContent:@""];
	[cell setIndexPath:indexPath];
	[cell getTextField].delegate = self;
	cell.selectionStyle = UITableViewCellSelectionStyleNone;
	
	if(indexPath.row == 0)
	{
		[cell setIconWithName:@"icon_lock"];
		[cell setPlaceHolder:NSLocalizedString(@"请输入原密码", @"请输入原密码")];
		[cell setReturnType:UIReturnKeyNext];
		[cell getTextField].text = @"";
		[cell getTextField].secureTextEntry = YES;
		self.oldPwdTextField = [cell getTextField];
	}
	else if(indexPath.row == 1)
	{
		[cell setIconWithName:@"icon_lock"];
		[cell setPlaceHolder:NSLocalizedString(@"请输入新密码", @"请输入新密码")];
		[cell setReturnType:UIReturnKeyNext];
		[cell getTextField].text = @"";
		[cell getTextField].secureTextEntry = YES;
		self.pwdTextField = [cell getTextField];
		
	}
	else if(indexPath.row == 2)
	{
		[cell setIconWithName:@"icon_lock"];
		[cell setPlaceHolder:NSLocalizedString(@"请重新输入密码", @"请重新输入密码")];
		[cell setReturnType:UIReturnKeyNext];
		[cell getTextField].text = @"";
		[cell getTextField].secureTextEntry = YES;
		self.pwdConfirmTextField = [cell getTextField];
	}
	[cell getTextField].indexPath= indexPath;
	[cell getTextField].delegate = self;
	return cell;
}

#pragma mark
- (void)textFieldDidChange:(NSNotification *)notification
{
	
	HOTextField *textField = (HOTextField *)notification.object;
	NSIndexPath *indexPath = ((HOTextField *)textField).indexPath;
	if(indexPath.row == 0)
	{
		self.replacedPassword = textField.text;
	}
	else if(indexPath.row == 1)
	{
		self.replacedPasswordCheck = textField.text;
	}
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
	if(![textField isKindOfClass:[HOTextField class]])
		return NO;
	HOTextField *hoTextField = (HOTextField *)textField;
	NSIndexPath *indexPath = hoTextField.indexPath;
	if((indexPath.row + 1) >= [self tableView:baseTableview numberOfRowsInSection:indexPath.section])
	{
		return NO;
	}
	
	NSIndexPath *nextIndexPath = [NSIndexPath indexPathForItem:(indexPath.row + 1) inSection:0];
	HOEditTableViewCell *cell = (HOEditTableViewCell *)[self tableView:baseTableview cellForRowAtIndexPath:nextIndexPath];
	if(cell)
	{
		[[cell getTextField] resignFirstResponder];
		[[cell getTextField] performSelector:@selector(becomeFirstResponder) withObject:nil afterDelay:0.1f];
		
		return NO;
	}
	return YES;
}
#pragma mark
- (void)requestDidSuccessWithResponse:(HOBaseResponseEntity *)response
{
	if([response isEqualAction:kHO_ACTION_ResetPassword])
	{
		self.view.userInteractionEnabled = YES;
		[self popupHintView:NSLocalizedString(@"密码修改成功", @"密码修改成功") inTime:1.5 needCallback:YES];
	}

	
}

- (void)hintDidHide
{
	self.view.userInteractionEnabled = YES;
	[self goBack];
}

@end
